package com.lqk.demo.concurrencytools.threadpool.juc.countdownlatch;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.lqk.demo.n2.util.Sleeper.sleep;

/**
 * @author lqk
 * @date 2022/1/19 10:21
 */
@Slf4j
public class TestCountDownLatch2 {
    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(4);
        CountDownLatch latch = new CountDownLatch(3);

        service.submit(()->{
            log.debug("begin...");
            sleep(1);
            latch.countDown();
            log.debug("end...{}",latch.getCount());
        });

        service.submit(()->{
            log.debug("begin...");
            sleep(1.5);
            latch.countDown();
            log.debug("end...{}",latch.getCount());
        });
        service.submit(()->{
            log.debug("begin...");
            sleep(2);
            latch.countDown();
            log.debug("end...{}",latch.getCount());
        });

        service.submit(()->{
            try {
                log.debug("waiting...");
                latch.await();
                log.debug("wait end...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }
}
